import { getListApi, addListApi, updataListApi, delListApi } from '@/api/exam'
export default {
  namespaced: true,
  state: {
    listArr: [],
    sort: 'desc',
    orderby: 'id',
    currentPage: 1,
    pageSize: 10,
    total: 0,
    searchVal: '',
  },
  mutations: {
    SET_LIST(state, data) {
      state.listArr = data.data
      state.total = data.total
    },
    SET_CURR(state, val) {
      state.currentPage = val
    },
    SET_PAGE(state, val) {
      state.pageSize = val
    },
    SET_SEARCH(state, val) {
      state.searchVal = val
    }
  },
  actions: {
    async getList({ commit, state }) {
      let r = await getListApi({
        sort: state.sort,
        orderby: state.orderby,
        currentPage: state.currentPage - 1,
        pageSize: state.pageSize,
        searchVal: state.searchVal
      })
      commit('SET_LIST', r)
    },
    async addList({ dispatch }, { form, uid}) {
      form.answers = JSON.stringify(form.answers)
      form.options = JSON.stringify(form.options)
      form.uid = uid
      console.log(form);
      let r = await addListApi(form)
      dispatch('getList')
    },
    async updateList({ dispatch }, form) {
      let r = await updataListApi(form)
      dispatch('getList')
    },
    async delList({ dispatch }, id) {
      let r = await delListApi(id)
      dispatch('getList')
    }
  }
}